package com.example.a2048game.diy

import android.app.AlertDialog
import android.content.Context
import android.content.DialogInterface
import android.graphics.Color
import android.graphics.Point
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.widget.GridLayout
import android.widget.Toast
import com.example.a2048game.MainActivity
import kotlin.math.abs
import kotlin.math.min
import kotlin.properties.Delegates

class  GameView
    @JvmOverloads  constructor(context: Context, attributeSet: AttributeSet?=null, defStyleAttr:Int=0):
    GridLayout(context,attributeSet) {
    private var cardWidthMix = 0
    private var arryofCard = Array(4){Array(4){Card(getContext())}}
    private var emptyPoint = ArrayList<Point>()
    private var mainActivity:MainActivity?=null

    var startx = 0F
    var starty = 0F

    init {
        columnCount = 4
        setBackgroundColor(0xffbbada0.toInt())
        post {
            // 获取宽高
            cardWidthMix = (width - 10) / 4
            addCard(cardWidthMix)
            // 打印宽高
            startGame()

            setOnTouchListener { view, motionEvent ->
                when(motionEvent.action){

                    MotionEvent.ACTION_DOWN -> {
                        startx =  motionEvent.x
                        starty =  motionEvent.y

                    }
                    MotionEvent.ACTION_UP -> {

                        val offsetx = motionEvent.x - startx
                        val offsety = motionEvent.y - starty

                        if (abs(offsetx) > abs(offsety)){
                            if(offsetx<=-5){
                                swipeLeft()
                            }else if (offsetx>5){
                                swipeRight()
                            }
                        }else {
                            if (offsety <= -5) {
                                swipeUp()
                            } else if (offsety >= 5) {
                                swipeDown()
                            }
                        }
                        view.performClick()
                    }
                }
                true
            }


        }

        }

    fun setMainActivity(mainActivity:MainActivity){
        this.mainActivity = mainActivity
    }



    private fun addCard(cardWidthMix:Int){
        for(y in 0 until 4){
            for(x in 0 until 4){
                val card = Card(context)
                card.setCardNumber(0)
                Log.d("LCD", "addCard: $cardWidthMix")
                addView(card,cardWidthMix,cardWidthMix)
                arryofCard[x][y] = card
            }
        }
    }

    fun startGame(){
        mainActivity!!.clearScore()
        for(y in 0 until 4){
            for(x in 0 until 4) {
                arryofCard[x][y].setCardNumber(0)
            }
        }
        addRandomNumber()
        addRandomNumber()
    }

    private fun addRandomNumber(){
        emptyPoint.clear()

        for(y in 0 until 4){
            for(x in 0 until 4){
                if (arryofCard[x][y].getCardNumber() <=0){
                    emptyPoint.add(Point(x,y))
                }
            }
        }
        val point = emptyPoint.removeAt((Math.random() * (emptyPoint.size)).toInt())
        val random = if (Math.random()>0.1) 2 else 4
        arryofCard[point.x][point.y].setCardNumber(random)

    }

    private fun swipeLeft(){

        var isNewNumber = false
        for(y in 0 until 4){
            var x = 0
            while (x<4){
               for(i in x+1 until  4){
                   if(arryofCard[i][y].getCardNumber()>0){
                       if(arryofCard[x][y].getCardNumber()<=0){
                           arryofCard[x][y].setCardNumber(arryofCard[i][y].getCardNumber())
                           arryofCard[i][y].setCardNumber(0)
                           x--
                           isNewNumber = true

                       }else if(arryofCard[x][y].equals(arryofCard[i][y])){
                           arryofCard[x][y].setCardNumber(arryofCard[x][y].getCardNumber()*2)
                           arryofCard[i][y].setCardNumber(0)
                           mainActivity!!.addScore(arryofCard[x][y].getCardNumber())
                           isNewNumber = true
                       }
                       break
                   }


               }
                x++
            }
        }
        if (isNewNumber){
            addRandomNumber()
            checkComplete()
        }

    }
    fun swipeRight(){

        var isNewNumber = false
        for(y in 0 until 4){
            var x = 3
            while (x>=0){
                for(i in x-1 downTo 0){
                    if(arryofCard[i][y].getCardNumber()>0){
                        if(arryofCard[x][y].getCardNumber()<=0){
                            arryofCard[x][y].setCardNumber(arryofCard[i][y].getCardNumber())
                            arryofCard[i][y].setCardNumber(0)
                            x++
                            isNewNumber = true
                        }else if(arryofCard[x][y].equals(arryofCard[i][y])){
                            arryofCard[x][y].setCardNumber(arryofCard[x][y].getCardNumber()*2)
                            arryofCard[i][y].setCardNumber(0)
                            mainActivity!!.addScore(arryofCard[x][y].getCardNumber())
                            isNewNumber = true

                        }
                        break
                    }

                }
                x--
            }
        }
        if (isNewNumber){
            addRandomNumber()
            checkComplete()
        }
    }
    fun swipeUp(){
        var isNewNumber = false
        for(x in 0 until 4){
            var y = 0
            while (y<4){
                for(i in y+1 until    4){
                    if(arryofCard[x][i].getCardNumber()>0){
                        if(arryofCard[x][y].getCardNumber()<=0){
                            arryofCard[x][y].setCardNumber(arryofCard[x][i].getCardNumber())
                            arryofCard[x][i].setCardNumber(0)
                            y--
                            isNewNumber = true
                        }else if(arryofCard[x][y].equals(arryofCard[x][i])){
                            arryofCard[x][y].setCardNumber(arryofCard[x][y].getCardNumber()*2)
                            arryofCard[x][i].setCardNumber(0)
                            mainActivity!!.addScore(arryofCard[x][y].getCardNumber())
                            isNewNumber = true
                        }
                        break
                    }
                }
                y++
            }
        }
        if (isNewNumber){
            addRandomNumber()
            checkComplete()
        }


    }
    fun swipeDown(){
        var isNewNumber = false
        for(x in 0 until 4){
            var y = 3
            while (y>=0){
                for(i in y-1 downTo   0){
                    if(arryofCard[x][i].getCardNumber()>0){
                        if(arryofCard[x][y].getCardNumber()<=0){
                            arryofCard[x][y].setCardNumber(arryofCard[x][i].getCardNumber())
                            arryofCard[x][i].setCardNumber(0)
                            y++
                            isNewNumber = true

                        }else if(arryofCard[x][y].equals(arryofCard[x][i])){
                            arryofCard[x][y].setCardNumber(arryofCard[x][y].getCardNumber()*2)
                            arryofCard[x][i].setCardNumber(0)
                            mainActivity!!.addScore(arryofCard[x][y].getCardNumber())
                            isNewNumber = true
                        }
                        break
                    }
                }
                y--
            }
        }
        if (isNewNumber){
            addRandomNumber()
            checkComplete()
        }
    }

    fun checkComplete(){

        var isComplete = true

    outer@for(y in 0 until 4){
            for(x in 0 until 4){
                if(arryofCard[x][y].getCardNumber()==0 || (x>0 && arryofCard[x][y].equals(arryofCard[x-1][y]))
                    || (x<3 && arryofCard[x][y].equals(arryofCard[x+1][y]))
                    || (y>0 && arryofCard[x][y].equals(arryofCard[x][y-1]))
                    || (y<3 && arryofCard[x][y].equals(arryofCard[x][y+1])))
                    {
                    isComplete = false
                    break@outer
                }
            }
        }
        if (isComplete){
            AlertDialog.Builder(getContext()).setTitle("你好").setMessage("您的分数为: "+mainActivity!!.getScore())
                .setPositiveButton("重来", DialogInterface.OnClickListener { dialog, which ->
                    startGame()
                    dialog.dismiss()
                }).show()
        }

    }

}